--
-- Author: mm
-- Date: 2018-04-17 11:59:54
--
-- ThirdLevelTipsPanel
-- 第三级提示：主界面右上角提示panel

local ClassRef = app.mvc.createPanelClass(...)

local listViewOriginHeight = nil -- 滚动列表原始高

--data =
function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.ThirdLevelTipsNode))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

--
function ClassRef:init(data)
    self.super.init(self, data)
    display.setCascadeOpacityEnabled( self, true )
    listViewOriginHeight = self._ui.ListView_1:getContentSize().height

    self._icon2InitSize = self._ui.icon_2:getContentSize()

    self:getTimeline():gotoFrameAndPause(0)

    -- self:refresh()
    self._datas = {}
    self._isInAction = false
end

-- data = {
--     remindId = ,
--     params =,
-- }
function ClassRef:addData(data)
    -- 等待的邮件只留一个
    if data.remindId == Constant.FUNC_TYPE_MAIL then
        for i,v in ipairs(self._datas) do
            if v.remindId == Constant.FUNC_TYPE_MAIL then
                table.remove(self._datas, i)
                break
            end
        end
    end
    self._datas[#self._datas + 1] = data

    if not self._isInAction then
        self:showNextTip()
    end
end

function ClassRef:showNextTip()
    local data
    if #self._datas > 0 then
        data = table.remove(self._datas, 1)
    end
    self:setVisible(data ~= nil)
    if not data then return end

    local params = data.params

    local titleStr, contentStr, iconPath
    local remindId = data.remindId
    local conf = GD:getRemindConfById( remindId )
    if conf.icon then
        iconPath = string.format("art/remind/%s.png", conf.icon)
    end
    if conf.name then
        titleStr = L(conf.name)
    end
    if conf.content then
        contentStr = L(conf.content)
    end
    if Constant.FUNC_TYPE_ACHIEVEMENT_GOT == remindId then -- 达成成就
        self._ui.text_res:setString( params.point or -1 )
        contentStr = " - " .. string.format( L(conf.content), L(params.name) )
        iconPath = GD:getImagePath( Res.achieveIconPath , params.icon)
        self._ui.icon_2:show()
        self._ui.icon_2:setContentSize(self._icon2InitSize)
    elseif Constant.FUNC_TYPE_CAPTAIN_ROOM == remindId then -- 解锁船长室藏品
        self._ui.text_res:setString("")
        contentStr = L(params.name)
        iconPath = params.iconPath.."/"..params.icon..".png"
        self._ui.icon_2:hide()
        self._ui.icon_2:setContentSize(cc.size(0, self._icon2InitSize.height))
    elseif Constant.FUNC_TYPE_MAIL == remindId then -- 新邮件

    elseif Constant.FUNC_TYPE_MERCENARY == remindId then
        self._ui.text_res:setString("")
        titleStr = string.format(titleStr, params.partnerName)
        contentStr = params.equipName or ""
        iconPath = params.iconPath
        self._ui.icon_2:hide()
        self._ui.icon_2:setContentSize(cc.size(0, self._icon2InitSize.height))
    elseif Constant.FUNC_TYPE_DUEL == remindId then
        self._ui.text_res:setString("")
        contentStr = string.format(contentStr, params.rankChange)
        self._ui.icon_2:hide()
        self._ui.icon_2:setContentSize(cc.size(0, self._icon2InitSize.height))
    elseif Constant.FUNC_TYPE_CROSS_MATCH == remindId then
        -- 跨服匹配提示信息
        contentStr = params
    end 

    local hasTitle = (titleStr ~= nil)
    self._ui.Panel_tips:setVisible(hasTitle)
    self._ui.text_tips:setVisible(not hasTitle)
    self._ui.icon:loadTexture(iconPath or Res.Sheep_Icon_Path)
    if hasTitle then
        self._ui.tlt:setString(titleStr)
        self._ui.text:setString(contentStr or "")

        -- TODO 这个做法有些奇怪而浪费
        local text_resWidth = self._ui.text_res:getVirtualRendererSize().width
        local icon2Width = self._ui.icon_2:getContentSize2()
        local textWidth = self._ui.text:getVirtualRendererSize().width
        -- print( " ------------------->>>>> 3个子控件宽 ", text_resWidth, icon2Width, textWidth )
        local widthTemp = text_resWidth + icon2Width + textWidth
        -- print( " ------------------->>>>> listView调整之后的宽 ", widthTemp )
        self._ui.ListView_1:setContentSize( cc.size( widthTemp, listViewOriginHeight ) )
        self._ui.ListView_1:forceDoLayout()
        self._ui.ListView_1:refreshView()
    else
        self._ui.text_tips:setString(contentStr or "")
    end

    -- action
    self:setOpacity( 255 )
    self:stopActionByTag( 998 )
    self:getTimeline():gotoFrameAndPlay(0, false)

    local delay = cca.delay(tonumber(conf.timeRemain or 2))
    local fade = cca.fadeOut(1)
    local cb = cca.callFunc(function()
        self._isInAction = false
        self:showNextTip()
    end)

    local action = cca.seq({delay, fade, cb})
    action:setTag(998)
    self:runAction(action)
    self._isInAction = true
end

function ClassRef:refresh()

end

return ClassRef
